
const PLAYER_IMG_URL = 'images/plane/hero.png';

define([
  'require', 'dragger', 'bullet'
], function (require, dragger, bullet) {
  'use strict';

  var _this = {};

  function init (app, container) {
    var self = _this;
    bullet.init(app, container);
    self.bullet = bullet;
    self.fireCount = 0;// 累计帧数，发射子弹；
    self.fireSpeed = 3;// 每秒钟子弹数量
    self.loading = true;

    new PIXI.loaders.Loader()
      .add('playerImg', PLAYER_IMG_URL)
      .load(function (loader, res) {
        self.playerTex = PIXI.Texture.from('playerImg');
        self.player = new PIXI.Sprite(self.playerTex);
        self.screen = app.screen;

        self.player.anchor.set(0.5);
        self.player.scale.set(0.5);
        self.player.position.x = self.screen.width / 2;
        self.player.position.y = self.screen.height - self.player.height / 2;

        self.player.interactive = true;
        self.player.buttonMode = true;
        dragger.applyDrag(self.player, {
          detectBounds: true,
          moveableArea: new PIXI.Rectangle(
            self.player.width / 2,
            self.player.height / 2,
            app.screen.width - self.player.width,
            app.screen.height - self.player.height
          )
        });

        container.addChild(self.player);
        self.loading = false;
      });
  }

  function update (delta) {
    if (_this.loading) return;
    // do nothing
    _this.fireCount += delta;
    if (_this.fireCount >= 60 / _this.fireSpeed) {
      _this.fireCount = 0;
      bullet.fireAt(_this.player.position);
      //explosion.explode(_this.player.position);
    }

    bullet.update(delta);
  }

  return {
    init: init,
    update: update,
    data: _this,
  }

});